package com.duyus.controller;

import com.duyus.pojo.Dept;
import com.duyus.pojo.Result;
import com.duyus.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Logger;

@Slf4j  // 通过注解实现 日志
@RequestMapping("/depts")  // 指定公共路径
@RestController
public class DeptController {

    // 输出日志的固定语句
    // private static Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    // 查询部门数据
//    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping  // 实现的效果和上面代码一样 限定get请求
    public Result list(){
        log.info("查询全部的部门数据");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    // 删除部门数据
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门:{}",id);
        deptService.delete(id);
        return Result.success();
    }

    // 添加部门数据
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门：{}",dept);
        deptService.add(dept);
        return Result.success();
    }

    //根据id更新部门
    @GetMapping("/{id}")
    public Result update(@PathVariable Integer id){
        log.info("根据id查看部门:{}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    //根据id更新部门
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("根据id修改部门:{}",dept.getId());
        deptService.modify(dept);
        return Result.success();
    }

}
